package com.beaven.imclient.presenter.user;

import android.text.TextUtils;
import com.beaven.imclient.R;
import com.beaven.imclient.data.CategorySelect;
import com.beaven.model.Factory;
import com.beaven.model.data.helper.UserHelper;
import com.beaven.model.data.user.CategoryPresenter;
import com.beaven.model.model.api.RspModel;
import com.beaven.model.model.api.user.CategoryModel;
import com.beaven.model.model.card.CategoryCard;
import com.beaven.model.model.db.Category;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import rx.Subscriber;
import rx.subscriptions.CompositeSubscription;
import timber.log.Timber;

/**
 * author: Beaven
 * time: 2017/5/31 9:45
 */
public class CategoriesPresenter
    implements CategoryContract.Presenter, CategoryPresenter.OnCategoryLoad {

  private CategoryPresenter categoryPresenter;
  private CompositeSubscription subscription;

  private CategoryContract.View view;

  private Map<String, CategorySelect> cateMap;

  public CategoriesPresenter(CategoryContract.View view) {
    this.view = view;
    subscription = new CompositeSubscription();
    categoryPresenter = new CategoryPresenter(this);
  }

  @Override public void subscribe() {
    cateMap = new HashMap<>();
    categoryPresenter.loadData();
  }

  @Override public void unSubscribe() {
    cateMap.clear();
    cateMap = null;
    categoryPresenter.destroy();
    subscription.clear();
  }

  @Override public void addCategory(String name) {
    if (!TextUtils.isEmpty(name)) {
      subscription.add(UserHelper.addCategory(new CategoryModel(name))
          .subscribe(new Subscriber<RspModel<CategoryCard>>() {
            @Override public void onCompleted() {

            }

            @Override public void onError(Throwable e) {
              view.showText(R.string.data_network_error);
              Timber.e(e);
            }

            @Override public void onNext(RspModel<CategoryCard> categoryCardRspModel) {
              if (categoryCardRspModel.isSuccess()) {
                List<CategoryCard> categoryCards = new ArrayList<>();
                categoryCards.add(categoryCardRspModel.getResult());
                Factory.getUserCenter().dispatchCategory(categoryCards);
              } else {
                view.showText(R.string.create_error);
              }
            }
          }));
    }
  }

  @Override public void loadCategory(List<Category> categories) {
    List<CategorySelect> selects = new ArrayList<>();
    if (cateMap != null && cateMap.size() != 0) {
      for (Category cate : categories) {
        CategorySelect select = cateMap.get(cate.getId());
        if (select == null) {
          select = CategorySelect.build(cate);
          cateMap.put(cate.getId(), select);
          selects.add(select);
        }
      }
    } else {
      for (Category cate : categories) {
        CategorySelect select = CategorySelect.build(cate);
        selects.add(select);
        cateMap.put(cate.getId(), select);
      }
    }
    view.showList(selects);
  }

  @Override public void notifyCate() {
    categoryPresenter.loadLocal();
  }
}
